package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartItem;
import com.atguigu.gmall.cart.service.CartBizService;
import com.atguigu.gmall.cart.vo.AddCartSuccessVo;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author:ahang
 * @create:2022-10-19 14:38
 * @Description:
 */
@RequestMapping("/api/inner/cart")
@RestController
public class CartRpcController {

    @Autowired
    CartBizService cartBizService;

    /**
     * 添加购物车
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/add/{skuId}/{skuNum}")
    public Result<AddCartSuccessVo> addToCart(@PathVariable("skuId") Long skuId,
                                              @PathVariable("skuNum") Integer skuNum){
        AddCartSuccessVo vo =  cartBizService.addToCart(skuId,skuNum);
        return Result.ok(vo);
    }

    /**
     * 删除被选中的商品
     * @return
     */
    @GetMapping("/delete/checked")
    public Result deleteChecked(){
        cartBizService.deleteChecked();
        return Result.ok();
    }

    /**
     * 返回购物车中被选中的商品集合
     * @return
     */
    @GetMapping("checked/items")
    public Result<List<CartItem>> getCheckedCartItems(){
        String cartKey = cartBizService.determinCartKey();
        List<CartItem> checkCartItems = cartBizService.getCheckCartItem(cartKey);
        return Result.ok(checkCartItems);
    }
}
